package Utils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MathUtils {

	public static double getVariance(ArrayList<Integer> counts) {
		double avg = getAverage(counts);
		int total = counts.size();
		double sum = 0.0;
		for (int i = 0; i < counts.size(); i++)
			sum += (counts.get(i) - avg) * (counts.get(i) - avg);
		return MathUtils.precision((sum) / (total), Constants.PRECISION);

	}

	/**
	 * compute average number of a list
	 * 
	 * @param intervals
	 * @return
	 */
	public static double getAverage(ArrayList<Integer> arrList) {
		double sum = 0;
		for (int i = 0; i < arrList.size(); i++) {
			sum += arrList.get(i);
		}
		return sum / arrList.size();
	}

	public static long str2Long(String timeStr) {
		long time = 0;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
			Date dt2 = sdf.parse(timeStr);
			time = dt2.getTime() / (1000 * 60 * 60);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return time;
	}

	public static Hashtable<String, Integer> countHashtable(ArrayList<String> result) {
		Hashtable<String, Integer> pair_count = new Hashtable<String, Integer>();
		for (int j = 0; j < result.size(); j++) {
			String pair = result.get(j);
			if (pair_count.containsKey(pair))
				pair_count.put(pair, pair_count.get(pair) + 1);
			else
				pair_count.put(pair, 1);
		}

		return pair_count;
	}

	/**
	 * normalize a list of data to reduce the differences of dimension(量纲)
	 * 计算公式：y=(x-MinValue)/(MaxValue-MinValue)
	 * 
	 * @author 韩 闻文
	 * @param dataList
	 * @return
	 */
	public static ArrayList<Double> normalization(ArrayList<Integer> dataList) {
		ArrayList<Double> result = new ArrayList<Double>();
		double max = Double.MIN_VALUE;
		double min = Double.MAX_VALUE;
		for (int i = 0; i < dataList.size(); i++) {
			int data = dataList.get(i);
			if (data > max)
				max = data;
			if (data < min)
				min = data;
		}
		for (int i = 0; i < dataList.size(); i++) {
			double item = (dataList.get(i) - min) / (max - min);
			result.add(item);
		}

		return result;
	}

	/**
	 * normalize a list of data to reduce the differences of dimension(量纲)
	 * 计算公式：y=(x-MinValue)/(MaxValue-MinValue)
	 * 
	 * @author 韩 闻文
	 * @param dataList
	 * @return
	 */
	public static double normalization(ArrayList<Integer> dataList, Integer param) {
		double max = Double.MIN_VALUE;
		double min = Double.MAX_VALUE;
		for (int i = 0; i < dataList.size(); i++) {
			int data = dataList.get(i);
			if (data > max)
				max = data;
			if (data < min)
				min = data;
		}
		double item = (double) (param - min) / (max - min);
		return item;
	}

	public static double precision(double pDouble, int digitNumber) {
		BigDecimal bd = new BigDecimal(pDouble);
		BigDecimal bd1 = bd.setScale(digitNumber, BigDecimal.ROUND_HALF_UP);
		pDouble = bd1.doubleValue();
		return pDouble;
	}

	public static boolean isDigit(String str) {
		Pattern pattern = Pattern.compile("^\\d+$");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	public static void main(String args[]) {
	}
}
